<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Brand;
use backend\models\AdminInfo;
use yii\data\Sort;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class BrandSearch extends Brand {
	
	public $grPBrandHead;
/**
     * @inheritdoc
     */
		 public function rules()
		    {    	
		       return [
			            [['id'], 'integer'],
			        	[['order','head_grp','useing'], 'integer'],
			            [['name'], 'string', 'max' => 24],
		       			 [['factory_id'], 'string', 'max' => 10],
			            [['memo'], 'string', 'max' =>255],
			        	[['tax_rate'],'number']	,
		       			[['grPBrandHead'],'safe']
		        ];
		    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
    	/* $sql="select grp_brand.*,grp_brand_head.name grPBrandHead,sys_image.url imgurl
                 from grp_brand left join grp_brand_head on grp_brand_head.id=grp_brand.head_grp
                left join sys_image on sys_image.id=grp_brand.img_id";
    	$query =Brand::findBySql($sql); */
    	$query=Brand::find()->joinWith("sysimage")->joinWith("grPBrandHead")->select("grp_brand.*,grp_brand_head.name grPBrandHead,sys_image.url imgurl");
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
	        'pagination'=>['pageSize' => '10'],
        ]);
        
        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'grp_brand.id' => $this->id,
        	'grp_brand.factory_id'=>$this->factory_id,
        	'grp_brand.tax_rate'=>$this->tax_rate	,
        		'grp_brand.useing'=>$this->useing
        ]);
        $query->andFilterWhere(['like', 'grp_brand.name', $this->name]) 
        		->andFilterWhere(['like', 'grp_brand.memo', $this->memo])
        		->andFilterWhere(['like', 'grp_brand.order', $this->order])
        		->andFilterWhere(['like', 'grp_brand_head.name', $this->grPBrandHead])
           ;
        return $dataProvider;
    }
    
    /**
     * 品牌分类查询
     */
    public function headsearch($params){
        $sql="select grp_brand_head.* , sys_image.url imgurl  from grp_brand_head 
            left join sys_image on sys_image.id=grp_brand_head.img_id order by `order` asc 
        ";
      //  $query=Brand::find()->joinWith("sysimage")->joinWith("grPBrandHead")->select("grp_brand.*,grp_brand_head.name grPBrandHead,sys_image.url imgurl");
        $query = BrandHead::find()->joinWith("sysImage")->select("grp_brand_head.* , sys_image.url imgurl")->orderBy("grp_brand_head.id asc");
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>['pageSize' => '10'],
        ]);
        $this->load($params);
        
        if (!$this->validate()) {
            return $dataProvider;
        }
        
        $query->andFilterWhere([
            'grp_brand_head.id' => $this->id,
        ]);
        $query->andFilterWhere(['like', 'grp_brand_head.name', $this->name])
        ;
        return $dataProvider;
    }

}
